"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.types import Integer, String, Text, Date, Enum, Boolean, Numeric
from sqlalchemy.orm import relation, relation, backref

from pylons.i18n import lazy_ugettext as _

from chowa.model.meta import Base
from chowa.model.layout import HomepageBlock

__all__ = ['Menu', 'MenuItem', 'MenuTopItem', 'MenuSubItem',
           'TabSet', 'Tab', 'HomepageBlockLink']


class Menu(Base):
    """Menus table."""
    __tablename__ = 'navigation_menu'
    
    id      = Column(Integer, primary_key=True)
    caption = Column(String(20))
    
    def __unicode__(self):
        return self.caption


class MenuItem(Base):
    """Abstract items table."""
    __tablename__ = 'navigation_menu_item'
    
    id = Column(Integer, primary_key=True)
    url = Column(Text, info={'verbose': _('Address')})
    caption = Column(String(30), info={'verbose': _('Caption')})
    hint = Column(String(100), info={'verbose': _('Hint')})
    weight = Column(Integer, info={'verbose': _('Weight')})
    is_disabled = Column(Boolean, default=False)
    subitems = relation('MenuSubItem', backref=backref('parent', remote_side='MenuItem.id'))
    
    level = Column(String(20))
    __mapper_args__ = {'polymorphic_on': level}
    
    def __unicode__(self):
        return self.caption
    
    def __repr__(self):
        return 'item'


class MenuTopItem(MenuItem):
    """Top level menu items table."""
    __mapper_args__ = {'polymorphic_identity': 'top-item'}
    menu_id = Column(Integer, ForeignKey(Menu.id), info={'verbose':_('Menu')})
    menu = relation(Menu, backref='items')


class MenuSubItem(MenuItem):
    """Sub-items table."""
    __mapper_args__ = {'polymorphic_identity': 'sub-item'}
    parent_id = Column(Integer, ForeignKey(MenuItem.id))


class TabSet(Base):
    __tablename__ = 'navigation_tabset'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(30))
    level = Column(Enum('primary', 'secondary'))


class Tab(Base):
    __tablename__ = 'navigation_tab'
    
    id = Column(Integer, primary_key=True)
    url = Column(String(255))
    prefix = Column(String(255))
    caption = Column(String(30))
    tabset_id = Column(Integer, ForeignKey(TabSet.id))
    
    tabset = relation(TabSet, backref='tabs')
    

class HomepageBlockLink(Base):
    __tablename__ = 'navigation_homepage_block_link'
    
    id = Column(Integer, primary_key=True)
    
    url = Column(Text, info={'verbose':_('Address')})
    caption = Column(String(30), info={'verbose':_('Caption')})
    weight = Column(Integer, info={'verbose':_('Weight')})
    is_disabled = Column(Boolean, default=False)
    block_id = Column(Integer, ForeignKey(HomepageBlock.id))
    
    block = relation(HomepageBlock, backref='links')
